package common;

import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


/**
 * @author lgq07612
 */
public class AsyncFlowBeanUtils {
    private static ApplicationContext applicationContext;


    /**
     * Spring容器会在创建该Bean之后，自动调用该Bean的setApplicationContext方法
     * @param applicationContext s
     */
    public static void setApplicationContext(@NotNull ApplicationContext applicationContext) {
        if(AsyncFlowBeanUtils.applicationContext == null) {
            AsyncFlowBeanUtils.applicationContext = applicationContext;
        }
    }

    /**
     * 获取applicationContext
     * @return a
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取 Bean
     * @param name 1
     * @return 1
     */
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean.
     * @param clazz a
     * @param <T> a
     * @return a
     */
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     * @param name a
     * @param clazz a
     * @param <T> a
     * @return a
     */
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }

}
